<table id="staff"></table>
<fieldset style="width:1065px;border:solid 1px #A4BED4;margin-bottom:10px;margin-top:10px;margin-left:-1px;position:relative;">
    <legend>员工管理操作</legend>
    <form action="/oa/staffAdd" id="staffSubmit">
        <div>
            <input type="hidden" name="id" id="ClickRowID" value="">
            <input type="hidden" name="old_login_id" id="old_login_id" value="">
            <table style="width:100%;table-layout:fixed;">
                <tbody>
                <tr>
                    <td style="width:100px;">选择登录帐号：</td>
                    <td style="width:150px;">
                        <input name="login_id" id="login_id" class="text" required="true">
                    </td>
                    <td style="width:100px;">员工姓名：</td>
                    <td style="width:250px;">
                        <input name="name" class="text required" id="name" required="true">
                    </td>
                </tr>

                <tr>
                    <td style="width:100px;">职位：</td>
                    <td style="width:250px;">
                        <input name="position_name" id="position_name" class="text required" required="true">
                    </td>
                    <td style="width:100px;">性别：</td>
                    <td style="width:150px;">
                        <input name="sex" type="radio" checked="checked" class="" value="男" >男&nbsp;
                        <input name="sex" type="radio" class="" value="女" >女
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">年龄：</td>
                    <td style="width:150px;">
                        <input name="age" class="text" id="age" >
                    </td>
                    <td style="width:100px;">手机号：</td>
                    <td style="width:150px;">
                        <input name="mobile_phone" id="mobile_phone" class="text required" required="true">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">出生年月：</td>
                    <td style="width:150px;">
                        <input name="birthday" id="birthday" class="text date">
                    </td>
                    <td style="width:100px;">座机电话：</td>
                    <td style="width:150px;">
                        <input name="tel" id="tel" class="text">
                    </td>
                </tr>
                <tr>

                </tr>

                <tr>
                    <td>备注：</td>
                    <td colspan="3"><textarea name="remark" id="ClickRowRemark" style="width:442px;height:40px;"></textarea></td>
                    <td colspan="2">
                        <a href="javascript:void(0)" class="button" id="SaveButton">
                            <span class="button-text">新增</span></a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="button" id="ResetButton">
                            <span class="button-text">重置</span>
                        </a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</fieldset>

<script type="text/javascript">
    $(document).ready(function(){

        // 初期化DataGrid
        $('#staff').datagrid({
            width:1080,
            height:250,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            url:'/oa/staffPattern',
            sortName: 'id',
            remoteSort: false,
            queryParams:{},
            idField:'id',
            pagination:true,
            rownumbers:true,
            pageList:[10,15,20],
            frozenColumns:[[
                {field:'ck',checkbox:true},
            ]],
            columns:[[
                {field:'name',title:'姓名',width:120},
                {field:'login_name',title:'登录帐号',width:120},
                {field:'agency_name',title:'所在机构',width:220,rowspan:2,sortable:true,
                    sorter:function(a,b){
                        return (a>b?1:-1);
                    }
                },
                {field:'department_name',title:'所在部门',width:150,rowspan:2},
                {field:'position_pname',title:'职位',width:150,rowspan:2},
                {field:'mobile_phone',title:'手机号',width:150,rowspan:2}
            ]],
            toolbar:
                    [{
                        id:'btnadd',
                        text:'新增员工记录',
                        iconCls:'icon-add',
                        handler:function(){
                            var rows = $('#staff').datagrid('getSelections');
                            if( rows.length == 1 ){
                                var rowData = rows[0];
                                $('#ClickRowID').val();
                                $('#name').val(rowData.name);
                                $('#position_name').val(rowData.position_pname);
                                $('#age').val(rowData.age);
                                if (rowData.sex == '男') {
                                    $("input[value='男']").attr('checked', 'checked');
                                } else {
                                    $("input[value='女']").attr('checked', 'checked');
                                }
                                $(".datebox input[type='text']").val(rowData.birthday);
                                $("input[name='birthday']").val(rowData.birthday);
                                $('#mobile_phone').val(rowData.mobile_phone);
                                $('#tel').val(rowData.tel);
                                $('#ClickRowRemark').val(rowData.remark);
                                $('#SaveButton span').text('新增');
                            }else{
                                parent.$.dooDialog.alert('您只能复制一条记录，请重新选择......');
                            }
                        }
                    },'-',{
                        text:'删除选中员工',
                        iconCls:'icon-remove',
                        handler:function(){
                            var ids  = "";

                            parent.$.dooDialog.confirm("你确定要删除这个员工吗？",function(){
                                var rows = $('#staff').datagrid('getSelections');
                                if( rows.length > 0 ){
                                    for(var i=0;i<rows.length;i++){
                                        if(i==0){
                                            ids = rows[i].id;
                                        }else{
                                            ids = ids + "," + rows[i].id;
                                        }
                                    }

                                    $.post('/oa/staffDel',{deleted:ids},function(data){
                                        if(data.code=='OK'){
                                            $("#staff").datagrid('reload');
                                        }else{
                                            parent.$.dooDialog.alert('员工删除失败！');
                                        }
                                    },'json');
                                }else{
                                    parent.$.dooDialog.alert('您还没有选择要删除的记录，请选择......');
                                }
                            });
                        }
                    },'-',{
                        text:'编辑选中员工',
                        iconCls:'icon-edit',
                        handler:function(){
                            var rows = $('#staff').datagrid('getSelections');
                            if( rows.length == 1 ){
                                var rowData = rows[0];
                                $('#ClickRowID').val(rowData.id);
                                $("input[name='old_login_id']").val(rowData.login_name);
                                $('#name').val(rowData.name);
                                $('#position_name').val(rowData.position_pname);
                                $('#age').val(rowData.age);
                                if (rowData.sex == '男') {
                                    $("input[value='男']").attr('checked', 'checked');
                                } else {
                                    $("input[value='女']").attr('checked', 'checked');
                                }
                                $(".datebox input[type='text']").val(rowData.birthday);
                                $("input[name='birthday']").val(rowData.birthday);
                                $('#mobile_phone').val(rowData.mobile_phone);
                                $('#tel').val(rowData.tel);
                                $('#ClickRowRemark').val(rowData.remark);
                                $('#SaveButton span').text('保存');
                            }else{
                                parent.$.dooDialog.alert('您只能编辑一条记录，请重新选择......');
                            }
                        }
                    }],
            onUnselect:function(rowIndex, field, value){
                $('#ClickRowID').val();
                $('#ClickRowName').val('');
                $('#ClickRowTypes').val('');
                $('#ClickRowBrand').val('');
                $('#ClickRowPattern').val('');
                $('#ClickRowRemark').val('');
            }
        });

        // 下拉框设置
        $('#login_id').combogrid({
            panelWidth:450,
            panelHeight:250,
            rownumbers:true,
            idField:'user',
            textField:'user',
            url:'/oa/userPattern',
            pagination:true,
            pageList:[7],
            columns:[[
                {field:'user',title:'登录帐号',width:150},
                {field:'agency',title:'机构',width:150},
                {field:'department',title:'部门',width:154}
            ]]
        });


        // 变更提交
        $('a#SaveButton').click(function(){
            // 数据检测
            var isValid = false;
            $("input.required").each(function(){
                if($(this).validatebox('isValid') == false){
                    isValid = true;
                    return false;
                }
            });
            if ($("#ClickRowID").val() == '' && $("input[name='login_id']").val() == '') {
                isValid = true;
                return false;
            }


            // 检测结果
            if(isValid){
                return false
            }

            // 数据提交
            $.post($('form#staffSubmit').attr('action'),$('form#staffSubmit').serializeArray(),function(data){
                if(data.code=='OK'){
                    $("#staff").datagrid('reload');
                    $('input.text,input.number,input.count').each(function(){
                        $(this).val('');
                    });
                    $('input[name="title"]').val('');
                    $('input[name="forecastDate"]').val('');
                    $('input[name="forecastTime"]').val('');
                    $('#ClickRowRemark').val('');
                    parent.$.dooDialog.alert('员工记录成功！');
                    return true;
                }else{
                    parent.$.dooDialog.alert(data.err);
                    return false;
                }
            },'json');
        });

        // 重置
        $('a#ResetButton').click(function(){
            $('input.text,input.number,input.count').each(function(){
                $(this).val('');
            });

            $('input[name="name"]').val('');
            $('input[name="position_name"]').val('');
            $('#ClickRowRemark').val('');
        });

        $('#ClickRowForecastTime').timespinner({
            showSeconds:true
        });
    });
</script>